package cn.zcscloud.mall.service.impl;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.mbg.mapper.PmsBrandMapper;
import cn.zcscloud.mall.mbg.mapper.PmsProductMapper;
import cn.zcscloud.mall.mbg.model.PmsBrand;
import cn.zcscloud.mall.mbg.model.PmsProduct;
import cn.zcscloud.mall.service.BrandService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: BrandServiceImpl
 * Description:
 * date: 2022/4/1 14:26
 *
 * @author zjf95
 * @since JDK 1.8
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private PmsBrandMapper pmsBrandMapper;

    @Autowired
    private PmsProductMapper pmsProductMapper;

    @Override
    public PageRequest page(String keyword, int pageNum, int pageSize) {

        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(keyword != null, PmsBrand::getName, keyword);
        queryWrapper.orderByDesc(PmsBrand::getSort);

        Page<PmsBrand> pmsBrandPage = new Page<>(pageNum, pageSize);

        pmsBrandMapper.selectPage(pmsBrandPage, queryWrapper);

        return PageRequest.init(pmsBrandPage);
    }

    /**
     * Description: 更新时的数据回显
     *
     * @author: zjf95
     * @date: 2022/4/1 16:59
     * @param:
     * @return:
     */
    @Override
    public PmsBrand findById(Long id) {

        PmsBrand pmsBrand = pmsBrandMapper.selectById(id);

        return pmsBrand;
    }

    @Override
    public void update(PmsBrand pmsBrand) {
        if (pmsBrand == null) {
            throw new RuntimeException("传输的数据为空");

        }
        pmsBrandMapper.updateById(pmsBrand);
    }

    @Override
    public void updateByIds(Integer[] ids, Integer factoryStatus) {
        List<Long> bids = Arrays.stream(ids).map(e -> Long.valueOf(e)).collect(Collectors.toList());

        //LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        //queryWrapper.eq(factoryStatus != null,PmsBrand::getFactoryStatus,factoryStatus);
        //queryWrapper.in(!bids.isEmpty(), PmsBrand::getId,bids);

        LambdaUpdateWrapper<PmsBrand> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(!bids.isEmpty(), PmsBrand::getId, bids)
                .set(factoryStatus != null, PmsBrand::getFactoryStatus, factoryStatus);

        pmsBrandMapper.update(null, updateWrapper);
    }

    @Override
    public void updateShow(Integer[] ids, Integer showStatus) {
        List<Long> bids = Arrays.stream(ids).map(Long::valueOf).collect(Collectors.toList());


        LambdaUpdateWrapper<PmsBrand> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(!bids.isEmpty(), PmsBrand::getId, bids)
                .set(showStatus != null, PmsBrand::getShowStatus, showStatus);

        pmsBrandMapper.update(null, updateWrapper);
    }

    @Override
    public void deleteById(Integer id) {

        //这个id就是brandId，删除的时候需要根据这个id查询是否需要同时删除品牌下面的商品
        LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsProduct::getBrandId,id);
        List<PmsProduct> pmsProducts = pmsProductMapper.selectList(queryWrapper);
        if (!pmsProducts.isEmpty() ){
            pmsProductMapper.deleteById(id);
        }
        //然后删除品牌
        pmsBrandMapper.deleteById(id);
    }

    @Override
    public void add(PmsBrand pmsBrand) {
        //品牌的名称应该是不能一样
        String brandName = pmsBrand.getName();
        LambdaQueryWrapper<PmsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(brandName != null,PmsBrand::getName,brandName);
        PmsBrand brand = pmsBrandMapper.selectOne(wrapper);

        if (brand != null){
            throw new RuntimeException("数据已存在");
        }
        //没有的话就进行添加
//PmsBrand [Hash = 153111422, id=null, name=dhbaukgdilwbvbjk, firstLetter=,
// sort=0, factoryStatus=0, showStatus=0, productCount=null, productCommentCount=null,
// logo=https://tanhuatest64.oss-cn-hangzhou.aliyuncs.com//2022/04/03/0c8a41e5-7bec-48bb-9c29-26112b547c0f.jpeg, bigPic=, brandStory=ddjakgdliub, serialVersionUID=1
        pmsBrand.setProductCount(0);
        pmsBrand.setProductCommentCount(0);

        pmsBrandMapper.insert(pmsBrand);
    }

}
